/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.cli;

import cz.mzk.kramerius.commons.DatastreamId;
import cz.mzk.kramerius.commons.Dom4jUtils;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class TestAction implements Action {

    private final FedoraConnector con;

    TestAction(FedoraConnector con) {
        this.con = con;
    }

    @Override
    public void run() {
        String first = "demo:999";
        String second = "demo:998";
        //removeObject(pid); //todo: remove demo:999
        //importTestObjects();
        objectExists(first);
        objectExists(second);

        addRelationship(first, "hates", second);
        removeRelationship(first, "hates", second);

        //printDatastream(pid, DatastreamId.DC);
    }

    private void removeObject(String pid) {
        con.removeObject(pid, "removing test object");
    }

    private void findObjects(String terms) {
        List<String> foundPids = con.findObjects(terms);
        System.out.println("found objects: " + foundPids.size());
        for (String foundPid : foundPids) {
            System.out.println(foundPid);
        }
    }

    private void importTestObjects() throws IOException {
        importObject("/home/rehan/fedora-old/foxmlExample.xml");
        //importObject("/home/rehan/fedora/foxmlExample2.xml");
    }

    private void importObject(String fileName) throws IOException {
        try {
            File file = new File(fileName);
            Document doc = Dom4jUtils.loadDocument(file, false);
            String pid = con.importFoxml(doc, "importing example foxml");
            System.out.println("pid: " + pid);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void objectExists(String pid) {
        boolean exists = con.objectExists(pid);
        System.out.println(pid + " exists: " + exists);
    }

    private void addRelationship(String subject, String predicate, String object) {
        //boolean added = con.addRelationship(pid1, "hates the shit", pid2);
        boolean added = con.addRelationship(subject, predicate, "kramerius", object);
        System.out.println("added: " + added);
    }

    private void printDatastream(String pid, DatastreamId datastreamId) {
        Document doc = con.getXmlDatastreamDissemination(pid, datastreamId);
        System.out.println(doc.asXML().toString());
    }

    private void removeRelationship(String subject, String predicate, String object) {
        boolean removed = con.removeRelationship(subject, predicate, "kramerius", object);
        System.out.println("removed: " + removed);
    }

    private void updateDatastreamFromFile(String pid, DatastreamId dsId, File dsFile) throws IOException {
        try {
            Document doc = Dom4jUtils.loadDocument(dsFile, false);
            con.updateDatastreamInlineContent(pid, dsId, doc);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
